package com.pucheng.bracelet.viewmodel.meaasge

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.LogUtils
import com.clj.fastble.exception.BleException
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.App
import com.pucheng.bracelet.app.ble.BleCmd
import com.pucheng.bracelet.app.ble.BleWriteAndNotifyCallback
import com.pucheng.bracelet.app.ble.ClientManager
import com.pucheng.bracelet.app.util.ByteUtils
import com.pucheng.bracelet.app.util.CacheUtil
import com.pucheng.bracelet.ui.adapter.PushData
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

class MsgPushViewModel : BaseViewModel() {
    private var data = mutableListOf<PushData>().apply {
        add(PushData(0, "电话", R.mipmap.ic_msg_phone, true))
        add(PushData(1, "信息", R.mipmap.ic_msg_msg, true))
        add(PushData(2, "QQ", R.mipmap.ic_msg_qq, true))
        add(PushData(3, "微信", R.mipmap.ic_msg_wchat, true))
        add(PushData(4, "更多消息推送", R.mipmap.ic_msg_more, true))
        if (App.instance.language != "zh"){
            add(PushData(5, "Facebook", R.mipmap.ic_msg_facebook, true))
            add(PushData(6, "Twitter", R.mipmap.ic_msg_twitter, true))
            add(PushData(7, "Whatsapp", R.mipmap.ic_msg_whats, true))
            add(PushData(8, "WeChat", R.mipmap.ic_msg_wchat, true))
            add(PushData(9, "Instagram", R.mipmap.ic_msg_instagram, true))
            add(PushData(10, "Skype", R.mipmap.ic_msg_skype, true))
            add(PushData(11, "Line", R.mipmap.ic_msg_line, true))
        }
    }

    var pushDataState: Int = 0

    var pushData = MutableLiveData<MutableList<PushData>>().apply {
        var push = CacheUtil.getPushMsgState()
        if (push.data.isNotEmpty()) {
            data.clear()
            data.addAll(push.data)
        }
        value = data
    }

    fun selectData(index: Int, pushData: PushData) {
        if (data.size > index) {
            data[index].selected = pushData.selected
            CacheUtil.setPushMsgState(Push(data))
        }

    }

    /**
     * 同步消息推送开关到手环
     */
    fun setPushDataToBle() {
        ClientManager.getInstance()
            .sendControlCmd(getPushCmd(), object : BleWriteAndNotifyCallback() {
                override fun onCharacteristicChanged(data: ByteArray?) {

                }

                override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
                }

                override fun onWriteFailure(exception: BleException?) {

                }
            })
    }

    /**
     * 获取闹钟指令
     */
    fun getPushCmd(): String {
        val pushStr = StringBuffer(BleCmd.PUSH_STATE)
        val list = pushData.value
        for (pushData in list!!) {
            pushDataState = if (pushData.selected) {
                ByteUtils.intAsIndexTo1(pushDataState, pushData.id)
            } else {
                ByteUtils.intAsIndexTo0(pushDataState, pushData.id)
            }
            pushStr.append(Integer.toBinaryString(pushDataState))
        }
        LogUtils.e("消息推送开关指令：$pushStr")
        return pushStr.toString()
    }

}

data class Push(val data: List<PushData> = arrayListOf())